package com.qingzhuge.member.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qingzhuge.controller.base.AbstractController;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.sys.dto.response.SysNoticeResponseDto;
import com.qingzhuge.sys.entity.SysNotice;
import com.qingzhuge.sys.entity.SysSiteConfig;
import com.qingzhuge.sys.service.ISysConfigService;
import com.qingzhuge.sys.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2019/1/10 0010 下午 12:00
 * @modified :
 * @since :
 */
@RestController
@RequestMapping("/api/open")
@Api(tags = "A首页数据")
public class ApiMainController extends AbstractController {
    @Resource
    private ISysConfigService configService;

    @Resource
    private ISysNoticeService noticeService;

    @ApiOperation(value = "系统信息")
    @GetMapping(value = "site")
    public ResponseEntity<ResponseBodyDto<HashMap<String, Object>>> doSite() {
        SysSiteConfig config = configService.getSiteConfig();
        HashMap<String, Object> site = Maps.newHashMap();
        site.put("name", config.getName());
        site.put("version", config.getVersion());
        site.put("addr", config.getAddr());
        site.put("enable", config.isEnable());
        return returnSuccess(site);
    }

    @ApiOperation(value = "系统公告")
    @GetMapping({"notice/{type}","notice"})
    public ResponseEntity<ResponseBodyDto<List<SysNoticeResponseDto>>> noticeList(@PathVariable(required = false) String type){
        if (StringUtils.isBlank(type)){
            type = "index";
        }
        List<SysNotice> notices = this.noticeService.findByType(type);
        List<SysNoticeResponseDto> noticeList = Lists.newArrayList();
        notices.forEach(notice->noticeList.add(noticeService.bean2Dto(notice, SysNoticeResponseDto.class)));
        return returnSuccess(noticeList);
    }
}
